import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {NzNotificationService} from "ng-zorro-antd/notification";
import {
${TableName}Service
} from "../../service/${table\-name}-service";
import {
${TableName}
} from "../../entity/${table\-name}";

@Component({
  selector: '${table\-name}-select',
  templateUrl: './${table\-name}-select.component.html',
  styleUrls: ['./${table\-name}-select.component.scss']
})
export class ${TableName}SelectComponent implements OnInit {

  constructor(private service: ${TableName}Service, private notificationService: NzNotificationService) {
  }
  @Input()
  visiable: boolean=false
  @Output()
  visiableChange = new EventEmitter()
  @Output()
  ok = new EventEmitter()
  queryParam: ${TableName!} = {
  }
  table: any = {
    dataSet: [],
    loading: false,
    page: {
      total: 0,
      pageIndex: 1,
      pageSize: 5,
      show: true,
    },
  }

  ngOnInit() {
    this.doSearch()
  }


  doSearch() {
    this.table.loading = true
    const param = {
      page: this.table.page.pageIndex - 1,
      size: this.table.page.pageSize,
    }
    this.service.page(param.page, param.size, this.queryParam).subscribe({
        next: res => {
          this.table.loading = false
          this.table.dataSet = res.result.content
          this.table.page.total = res.result.totalElements

        },
        error: err => {
          console.error(err)
          this.table.loading = false
          this.notificationService.warning("warning", "request fail")
        }
      }
    )
  }



  pageChange(index: number) {
    this.table.page.pageIndex = index
    this.doSearch()
  }
  handleOk(item:any): void {
    this.ok.emit(item)
    this.closeModal()
  }

  handleCancel(): void {
    this.closeModal()
  }

  closeModal() {
    this.reset()
    this.visiableChange.emit(false)
  }
  reset(){
    this.queryParam={}
  }
}
